<?php

/*
 * Project: oCoder Framework
 * File: /models/home.php
 * Purpose: model for the home controller.
 * Author: Trung Hoang
 */

class HomeModel extends BaseModel {

    //data passed to the home index view
    public function index() {
        $this->viewModel->set("pageTitle", "oCoder Framework");
        $this->viewModel->set('listNews', $listNews);
        return $this->viewModel;
    }

    public function timetable($weekNumber) {
        if (empty($weekNumber)) {
            $dateNow = new DateTime();
            $day = $dateNow->format("w");

            $week_start = date('Y-m-d', strtotime('-' . ($day - 1) . ' days'));
            $week_end = date('Y-m-d', strtotime('+' . (6 - $day) . ' days'));
        } else {
            echo "QTTT";
        }
        //echo $temp_date = date('Y-m-d', strtotime($week_start));

        $lstClass = $this->find_all("classes", "classes_id DESC", "", "*");
        $this->viewModel->set("lstClass",$lstClass);
        $lstSchedule = $this->find_all("schedule AS sh LEFT JOIN lession AS l ON sh.lession_id = l.lession_id LEFT JOIN subject AS sb ON sh.subject_id = sb.subject_id LEFT JOIN classes AS c ON sh.classes_id = c.classes_id", "sh.dates ASC", "sh.dates >= '{$week_start}' AND sh.dates <= '{$week_end}'", "sh.schedule_id, l.lession_name, l.theory, l.lab, sb.subject_name, sb.keyword AS keyword_subject, c.classname, c.keyword AS keyword_class, sh.dates");
        $this->viewModel->set("lstSchedule", $lstSchedule);
        $categories = $this->find_all("categories", "category_id DESC", "", "*", $limit = '');
        $this->viewModel->set("categories", $categories);
        $this->viewModel->set("pageTitle", "Thời khóa biểu");

        $a = date("Y-m-d", strtotime($week_start . '+1 weeks')); //sau 1 tuần
        $this->viewModel->set("setInputFrom", $a);
        return $this->viewModel;
    }

    public function getList($class, $fromDate, $toDate) {
        $where = "";
        if ($class != -1) {
            $where .= "sh.classes_id = {$class}";
        }
        if (!empty($fromDate) && !empty($toDate)) {
            if($class != -1){
                $where .= " AND ";
            }
            $where .= "sh.dates >= '{$fromDate}' AND sh.dates <= '{$toDate}'";
        }

        $lstSchedule = $this->find_all("schedule AS sh LEFT JOIN lession AS l ON sh.lession_id = l.lession_id LEFT JOIN subject AS sb ON sh.subject_id = sb.subject_id LEFT JOIN classes AS c ON sh.classes_id = c.classes_id", "sh.dates ASC", $where, "sh.schedule_id, l.lession_name, l.theory, l.lab, sb.subject_name, sb.keyword AS keyword_subject, c.classname, c.keyword AS keyword_class, sh.dates");
        $this->viewModel->set("lstSchedule", $lstSchedule);
        return $this->viewModel;
    }

    public function viewDetail($id) {
        $news = $this->find('news', $orderby = 'news_id DESC', $where = 'news_id = ' . $id, $cols = '*', $limit = '');
        $this->viewModel->set("news", $news);
        
        $relate = $this->find_all("news", "news_id DESC", "news_id != {$id} AND category_id = {$news->category_id}", "*", "7");
        $this->viewModel->set("relatePage", $relate);
        
        //$this->viewModel->set('listNews', $listNews);
        $categories = $this->find_all("categories", "category_id DESC", "", "*", $limit = '');
        $this->viewModel->set("categories", $categories);
        $this->viewModel->set("pageTitle", $news->title);
        return $this->viewModel;
    }
    
    public function Contact(){
        $categories = $this->find_all("categories", "category_id DESC", "", "*", $limit = '');
        $this->viewModel->set("categories", $categories);
        $this->viewModel->set("pageTitle", "Liên hệ");
        return $this->viewModel;
    }

     public function AboutUs(){
        $categories = $this->find_all("categories", "category_id DESC", "", "*", $limit = '');
        $this->viewModel->set("categories", $categories);
        $this->viewModel->set("pageTitle", "Giới thiệu");
        return $this->viewModel;
    }

    public function getDateToDate($fromDate = "", $endDate = "", $class = "") {
        $where = "";
        if (empty($fromDate) && empty($endDate)) {
            $where = "sh.dates >= NOW()";
        } else {
            $where = (empty($fromDate)) ? " sh.dates >= NOW() " : " sh.dates >= '" . substr($fromDate, 4) . "-" . substr($fromDate, 2, 2) . "-" . substr($fromDate, 0, 2) . "'";
            $where .= (empty($endDate)) ? "" : " AND sh.dates <= '" . substr($endDate, 4) . "-" . substr($endDate, 2, 2) . "-" . substr($endDate, 0, 2) . "'";
        }

        if (!empty($class)) {
            $where .= " AND sh.classes_id = {$class}";
        }

        //echo $where;
        //die();
        $sch = $this->find_all("schedule AS sh LEFT JOIN lession AS l ON sh.lession_id = l.lession_id LEFT JOIN subject AS sb ON sh.subject_id = sb.subject_id LEFT JOIN classes AS c ON sh.classes_id = c.classes_id", "sh.dates ASC", "{$where}", " sh.schedule_id, l.lession_name, l.theory, l.lab, sb.subject_name, sb.keyword AS keyword_subject, c.classname, c.keyword AS keyword_class, sh.dates");
        $this->viewModel->set("schJson", $sch);
        return $this->viewModel;
    }

    public function viewsSchedule($id, $page) {
        $categories = $this->find_all("categories", "category_id DESC", "", "*", $limit = '');
        $this->viewModel->set("categories", $categories);

        //get data from table schedule
        $sch = $this->find_all("schedule AS sh LEFT JOIN lession AS l ON sh.lession_id = l.lession_id LEFT JOIN subject AS sb ON sh.subject_id = sb.subject_id LEFT JOIN classes AS c ON sh.classes_id = c.classes_id", "sh.dates ASC", "sh.schedule_id = {$id}", " sh.schedule_id, l.lession_name, l.theory, l.lab, sb.subject_name, sb.keyword AS keyword_subject, c.classname, c.keyword AS keyword_class, sh.dates");


        $this->viewModel->set("scheduleUpdate", $sch);
        //get data from table subject
        $sbj = $this->find_all("subject", "subject_id ASC", "", "subject_id, subject_name");
        $this->viewModel->set("subjectUpdate", $sbj);

        //get data from table class
        $cls = $this->find_all("classes", "classes_id ASC", "", "classes_id, classname");
        $this->viewModel->set("classUpdate", $cls);

        //get data from table lession

        $lss = $this->find_all("lession", "sortOrder ASC", "", "lession_id, lession_name");
        $this->viewModel->set("lessionUpdate", $lss);

        $this->viewModel->set("pageTitle", "Update");
        return $this->viewModel;
    }

    public function viewsContent($id, $page) {
        //echo $page;
        
        if(empty($page)){
            $lmStart=0;
            $lmEnd=15;
        }else{
            $lmStart=($page-1)*15;
            $lmEnd = ($page*15)-1;
            
        }
        
        $table = 'news';
        $condition = " category_id = {$id}";
        $countPage = $this->find("news","news_id",$condition,"COUNT(*) AS totalRecord");
        $this->viewModel->set("totalPage",$countPage);

      
//
//        $limit = 10;
//
//        $offset = ($page - 1) * $limit;
//
//        $strLimit = "$offset, $limit";
        $listNews = $this->find_all($table, 'news_id DESC', $condition, "*", "{$lmStart}, {$lmEnd}");
        
        $this->viewModel->set('listNews', $listNews);
        $categories = $this->find_all("categories", "category_id DESC", "", "*", $limit = '');
        $title = $this->find('categories',"category_id DESC",$condition,"*",1);
        $this->viewModel->set("categories", $categories);
        $this->viewModel->set("pageTitle",$title->category_name);

        return $this->viewModel;
    }

    public function home() {
        $this->viewModel->set("pageTitle", "Trang chu");
        
        
        $listCouse = $this->find_all("news", "news_id DESC", "category_id = 9", "*","4");
        //lay slide ne
        $listSlide = $this->find_all('news', $orderby = 'news_id DESC', $where = '', $cols = '*', $limit = '0, 5');

        //lay tin moi ne
        $listNewsest = $this->find_all('news', $orderby = 'news_id DESC', $where = '', $cols = '*', $limit = '0, 9');

        //lay ds chuyen muc ne
        $categories = $this->find_all("categories", "category_id DESC", "", "*", $limit = '');
        $listNews = array();

        //lay ds tin tuc tyheo chuyen muc ne
        foreach ($categories as $category) {
            $news = $this->find_all('news', $orderby = 'news_id DESC', $where = 'category_id = ' . $category->category_id, $cols = '*', $limit = '');

            $listNews[$category->category_id] = $news;
        }

        $this->viewModel->set("listSlide", $listSlide);
        $this->viewModel->set("newsest", $listNewsest);
        $this->viewModel->set("lstCount", $listCouse);
        $this->viewModel->set("categories", $categories);
        $this->viewModel->set('listNews', $listNews);
        $this->viewModel->set("listNews", $listNews);
        $this->viewModel->set("listNews", $listNews);
        // gio qua  lay out, foreach ra may cai nay la ok
        return $this->viewModel;
    }

}

?>
